package com.ygqh.baby.controller.admin;

import java.io.IOException;
import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSON;
import com.ygqh.baby.ao.FileType;
import com.ygqh.baby.ao.Message;
import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.ao.ResultSet;
import com.ygqh.baby.controller.utils.DocumentHandler;
import com.ygqh.baby.po.AdminInvoice;
import com.ygqh.baby.po.AdminSupplierFrame;
import com.ygqh.baby.service.AdminSkuService;
import com.ygqh.baby.service.AdminSupplementContractService;
import com.ygqh.baby.service.AdminSupplierFrameService;
import com.ygqh.baby.service.FileService;
import com.ygqh.baby.service.YgCategoryService;
import com.ygqh.baby.shiro.utils.SessionUtil;



@Controller
@RequestMapping("/admin/frame")
public class FrameController extends BaseController {
	@Autowired
	private YgCategoryService categoryService;
	@Autowired
	protected FileService fileService;
	@Autowired
	private AdminSupplierFrameService adminSupplierFrameService;

	@Autowired
	private AdminSkuService adminSkuService;
	@Autowired
	private AdminSupplementContractService supplementContractService;


	@RequestMapping("searchFrame")
	@ResponseBody
	public Map<String, Object> searchFrame(String q, QueryInfo queryInfo)
			throws ParseException {

		ResultSet<AdminSupplierFrame> resultSet = adminSupplierFrameService.search(queryInfo, q);

		return resultSet.toDatatables(null);
	}
	@RequestMapping("saveFrame")	
	@ResponseBody	
	public Message saveFrame(String frame,HttpServletRequest request,HttpServletResponse response) {
		AdminSupplierFrame adminSupplierFrame=JSON.parseObject(frame, AdminSupplierFrame.class);
		if(adminSupplierFrame.getId()==null){
			adminSupplierFrame.setCreateBy(getSession().getTrueName());
			adminSupplierFrame.setCreateTime(new Date());
		}else{
			adminSupplierFrame.setUpdateBy(getSession().getTrueName());
			adminSupplierFrame.setUpdateTime(new Date());
		}
		adminSupplierFrameService.saveOrUpdate(adminSupplierFrame);
		return SUCCESS_MESSAGE;
		
	}

	@RequestMapping("updateFrameStatus")
	@ResponseBody
	public Message updateFrameStatus(Long id,String frameCode) {
		AdminSupplierFrame frame = new AdminSupplierFrame();
		frame.setId(id);
		frame.setFrameCode(frameCode);
		frame.setFrameStatus("Archived");
		frame.setUpdateTime(new Date());
		frame.setUpdateBy(SessionUtil.getCurrentUser().getTrueName());
		
		adminSupplierFrameService.update(frame);
		return Message.success(null);
	}

	@RequestMapping("findDetailById")
	@ResponseBody
	public AdminSupplierFrame findDetailById(Long id) {

		return adminSupplierFrameService.findDetailById(id);
		
	}

	/**
	 * 框架删除
	 * 
	 * @param id
	 * @return
	
	 */
	@RequestMapping("deleteFrame")
	@ResponseBody
	public Message deleteFrame(@RequestParam Long id)  {
		String operator = SessionUtil.getCurrentUser().getTrueName();
		Message message = adminSupplierFrameService.deleteSoft(id,operator);
		return message;
	}
	@RequestMapping("downloadFrame")
	public void downloadFrame(HttpServletRequest request, HttpServletResponse response,String frameCode,FileType fileType) throws IOException {
		byte[] bytes = fileService.downloadFromOss(fileType+"/"+frameCode+".pdf");
		DocumentHandler.createDownload(bytes, frameCode+".pdf", request, response);
	}
	
	@RequestMapping("upLoadFrame")
	@ResponseBody
	public Message upLoadContract(Long id,String frameCode,FileType fileType,@RequestParam("uploadFile")   MultipartFile uploadFile, HttpServletRequest request, HttpServletResponse response) throws IOException {
		
		String url=fileService.upload2Oss(fileType, uploadFile,frameCode);
		if(url.length()>0){
		
				AdminSupplierFrame adminContract=new AdminSupplierFrame();
				adminContract.setId(id);
				adminContract.setIsUpload(true);
				adminSupplierFrameService.update(adminContract);
			
		}
		return SUCCESS_MESSAGE;
	}
	@RequestMapping("isExistsFrame")
	@ResponseBody
	public Boolean isExistsFrame(Long id,String frameCode)  {
		return adminSupplierFrameService.findDetailById(id,frameCode);
	}
	@RequestMapping("getFrameCode")
	@ResponseBody
	public List<AdminSupplierFrame> getFrameCode(Long supplierId){
		return adminSupplierFrameService.findFrameCode(supplierId);
		
	}
	

}
